//
//  test.swift
//  elarc01
//
//  Created by elarc on 16/3/22.
//  Copyright © 2016年 elarc. All rights reserved.
//

import UIKit

class CustomAnimationController: NSObject, UIViewControllerAnimatedTransitioning {
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
        let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
        let finalFrame = transitionContext.finalFrameForViewController(toViewController!)
        
        let containerView = transitionContext.containerView()
        
        let screenBounds = UIScreen.mainScreen().bounds
        toViewController?.view.frame = CGRectOffset(finalFrame, 0, screenBounds.size.height)
        
        let toViewView = toViewController?.view
        containerView!.addSubview(toViewView!)
        
        //动画
        let duration = self.transitionDuration(transitionContext)
        
        UIView.animateWithDuration(duration, animations: {
            fromViewController?.view.alpha = 0.5
            toViewController?.view.frame = finalFrame
            }, completion: {(finished) in
                fromViewController?.view.alpha = 1.0
                transitionContext.completeTransition(true)
        })
        
    }
}